//
//  CameraSettingCell.swift
//  WatermarkCamera
//
//  Created by 赵新 on 8/7/2024.
//

import UIKit
import AxcBedrock

class CameraSettingCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .white
        makeUI()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    lazy var rightDisTitleLabel: UILabel = {
        let label = UILabel.Axc.Create(font: 17,
                                       color: UIColor.white,
                                       alignment: .right)
        label.isHidden = true
        return label
    }()

    lazy var bottomDisTitleLabel: UILabel = {
        let label = UILabel.Axc.Create(font: 17,
                                       color: UIColor.white,
                                       alignment: .left)
        label.isHidden = true
        return label
    }()

    lazy var switchBtn: UISwitch = {
        let switc = UISwitch()
        switc.onTintColor = "5290FD".axc.uiColor
        switc.isHidden = true
        switc.addTarget(self, action: #selector(onSwitchAction(sender:)), for: .valueChanged)
        return switc
    }()

    lazy var redPointView: UIView = {
        let view = UIView()
        view.backgroundColor = "F54A45".axc.uiColor
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 4
        return view
    }()

    lazy var titleImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    var model: CameraSettingVM.CellModel?

    var switchDidChange: ((Bool, CameraSettingVM.CellModel) -> Void)?

    @objc
    func onSwitchAction(sender: UISwitch) {
        guard let model = model else { return }
        switchDidChange?(sender.isOn, model)
    }

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = "323233".axc.uiColor
        return label
    }()

    /// 箭头
    lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView(image: "arrow_right".axc.uiImage)
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    func makeUI() {
        let orange = 0
        var guava = 1
        if orange == 0 { guava = 0 }
        guard guava == 0 else { print(""); return }
        titleLabel.textColor = "323233".axc.uiColor
        titleLabel.font = 17.axc.uiFont
        addSubview(titleLabel)
        addSubview(titleImageView)
        addSubview(arrowImageView)
        addSubview(rightDisTitleLabel)
        addSubview(bottomDisTitleLabel)
        addSubview(switchBtn)
        addSubview(redPointView)
        addSubview(lineView)

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(16.5)
            make.left.equalTo(StandardEdge.left)
            make.height.equalTo(21)
        }

        titleImageView.snp.makeConstraints { make in
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right).offset(8)
            make.size.equalTo(1.axc.cgSize)
        }

        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-StandardEdge.right)
            make.size.equalTo(12)
        }

        rightDisTitleLabel.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.right.equalTo(arrowImageView.snp.left).offset(-8)
        }
        let strawberry = 0
        var mandarin = 1
        if strawberry == 0 { mandarin = 0 }
        guard mandarin == 0 else { print(""); return }
        bottomDisTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.bottom.equalTo(-16)
            make.right.equalTo(arrowImageView.snp.left).offset(-8)
        }

        switchBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-StandardEdge.right)
        }

        redPointView.snp.makeConstraints { make in
            make.top.right.equalTo(switchBtn)
            make.size.equalTo(8)
        }
        let imbe = 0
        var quince = 1
        if imbe == 0 { quince = 0 }
        guard quince == 0 else { print(""); return }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(StandardEdge.left)
            make.bottom.equalToSuperview()
            make.right.equalTo(-StandardEdge.right)
            make.height.equalTo(0.5)
        }
    }

    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = "E6E6E6".axc.uiColor
        return view
    }()

    func configModel(_ model: CameraSettingVM.CellModel) {
        self.model = model

        titleLabel.text = model.title
        // 样式
        arrowImageView.isHidden = true
        rightDisTitleLabel.isHidden = true
        bottomDisTitleLabel.isHidden = true
        switchBtn.isHidden = true
        redPointView.isHidden = true
        titleImageView.isHidden = true
        switch model.style {
        case .arrow:
            arrowImageView.isHidden = false
        case let .text(text, color: color, font: font, direction: direction):
            arrowImageView.isHidden = false
            switch direction {
            case .bottom:
                bottomDisTitleLabel.isHidden = false
                bottomDisTitleLabel.text = text
                bottomDisTitleLabel.font = font
                bottomDisTitleLabel.textColor = color
            case .right:
                rightDisTitleLabel.isHidden = false
                rightDisTitleLabel.text = text
                rightDisTitleLabel.font = font
                rightDisTitleLabel.textColor = color
            default: break
            }
        case let .textSwitch(text, color: color, font: font, direction: direction, isOn: isOn, redPoints: redPoints):
            switchBtn.isHidden = false
            switchBtn.isOn = isOn
            redPointView.isHidden = !redPoints
            switch direction {
            case .bottom:
                bottomDisTitleLabel.isHidden = false
                bottomDisTitleLabel.text = text
                bottomDisTitleLabel.font = font
                bottomDisTitleLabel.textColor = color
            case .right:
                rightDisTitleLabel.isHidden = false
                rightDisTitleLabel.text = text
                rightDisTitleLabel.font = font
                rightDisTitleLabel.textColor = color
            default: break
            }
        case let .switch(isOn, redPoints: redPoints):
            switchBtn.isHidden = false
            switchBtn.isOn = isOn
            redPointView.isHidden = !redPoints
        case let .titleImageText(image, imageSize: imageSize, text: text, color: color, font: font, direction: direction):
            titleImageView.isHidden = false
            titleImageView.image = image
            titleImageView.snp.updateConstraints { make in
                make.size.equalTo(imageSize)
            }
            arrowImageView.isHidden = false
            switch direction {
            case .bottom:
                bottomDisTitleLabel.isHidden = false
                bottomDisTitleLabel.text = text
                bottomDisTitleLabel.font = font
                bottomDisTitleLabel.textColor = color
            case .right:
                rightDisTitleLabel.isHidden = false
                rightDisTitleLabel.text = text
                rightDisTitleLabel.font = font
                rightDisTitleLabel.textColor = color
            default: break
            }
        }
    }
}
